<?php

namespace App\Http\Controllers\Client;

use App\Models\Message;
use Illuminate\Support\Facades\Auth;
use App\Repositories\MessageRepository;
use App\Http\Controllers\Controller;
use App\Http\Requests\Client\Message\ShowRequest;
use App\Http\Requests\Client\Message\QueryResource;
use App\Http\Requests\Client\Message\MessageCreate;
use App\Http\Requests\Client\Message\MessageUpdate;
use App\Http\Requests\Client\Message\MessageDestroy;

class MessageController extends Controller
{
    protected $messageRepository;

    /**
     * MessagesController constructor.
     * @param MessageRepository $messageRepository
     */
    public function __construct(
        MessageRepository $messageRepository
    )
    {
        $this->messageRepository = $messageRepository;
    }

    /**
     * 获取消息模板列表资源列表
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $service_id = $request->get('service_id', false);
        $limit = $request->get('area_id', false);
        $messages = $this->messageRepository->getMessages($service_id, $limit);
        return success($messages);
    }

    /**
     * 获取消息模板详情
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, Message $message)
    {
        return success($message);
    }


    /**
     * 创建消息模板
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param MessageCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(MessageCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $messages = $this->messageRepository->create($attributes);
        return stored($messages);
    }

    /**
     * 更新消息模板资源
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param MessageUpdate $request
     * @param message $message
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(MessageUpdate $request, Message $message)
    {
        $messagesAttr = $request->only([
            'content',
            'status',
        ]);
        $message->update($messagesAttr);
        return updated($message);
    }

    /**
     * 删除 消息模板 资源
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param MessageDestroy $request
     * @param Message $message
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(MessageDestroy $request, Message $message)
    {
        $force = $request->get('force');
        $force ? $message->delete() : $message->forceDelete();
        return deleted();
    }

    /**
     * 获取短信模板
     *
     * Date: 2018/05/25
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function template(QueryResource $request)
    {
        $service_id = $request->get('service_id', false);
        $query = Message::query();
        if ($service_id !== false) {
            $query->where('service_id', '=', $service_id);
        }
        $messages = $query->first();
        return $messages;
    }
}
